#include "deletebackupdialog.h"
#include "ui_deletebackupdialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPainter>
#include <unistd.h>
#include "xatom-helper.h"
#include "globalbackupinfo.h"
#include "../common/mydefine.h"
#include "../common/utils.h"
#include "messageboxutils.h"
#include <ukuistylehelper/ukuistylehelper.h>
#include <windowmanager/windowmanager.h>

DeleteBackupDialog::DeleteBackupDialog(ParseBackupList::BackupPoint backupPonit, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DeleteBackupDialog),
    m_backupPonit(backupPonit),
    m_pInterface(nullptr)
{
    ui->setupUi(this);
    setMinimumSize(450, 138);
    setMaximumSize(450, 200);
    this->setAutoFillBackground(true);

    // 添加窗管协议
    if (GlobelBackupInfo::inst().isWayland()){
        // 去除窗管标题栏，传入参数为QWidget *
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    } else {
        MotifWmHints hints;
        hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
        hints.functions = MWM_FUNC_ALL;
        hints.decorations = MWM_DECOR_BORDER;
        XAtomHelper::getInstance()->setWindowMotifHint(winId(), hints);
    }
    setWindowModality(Qt::WindowModal);

    // 设置标题栏
    QHBoxLayout * titleLayout = new QHBoxLayout();

    // logo
    m_mTitleIcon  = new PixmapLabel;
    m_mTitleIcon->setFixedSize(24, 24);
    m_mTitleIcon->setThemeIconSchema(THEME_YHKYLIN_BACKUP_TOOLS, ":/images/yhkylin-backup-tools.png");

    // 标题
    m_titleLabel  = new MyLabel;
    m_titleLabel->setFixedSize(380, 30);
    // 正在删除数据，请稍候
    m_titleLabel->setDeplayText(tr("Please wait while data is being removed"));
    m_titleLabel->setToolTip(tr("Please wait while data is being removed"));

    // 关闭按钮
    m_buttonClose = new QPushButton;
    m_buttonClose->setProperty("isWindowButton", 0x2);
    m_buttonClose->setProperty("useIconHighlightEffect", 0x8);
    m_buttonClose->setFlat(true);
    m_buttonClose->setFixedSize(30, 30);
    m_buttonClose->setIcon(QIcon::fromTheme("window-close-symbolic"));

    titleLayout->addWidget(m_mTitleIcon);
    titleLayout->addWidget(m_titleLabel);
    titleLayout->addStretch();
    titleLayout->addWidget(m_buttonClose);

    // 提示信息
    m_labelMessage = new MyLabel;
    m_labelMessage->setIsOriginal(true);
    m_labelMessage->setWordWrap(true);
    // 正在删除备份点
    m_labelMessage->setDeplayText(tr("Removing backup point..."));

    // 等待图标
    m_loadingGif = new QLabel;
    m_loadingGif->setFixedSize(24,24);
    // 等待动画
    m_movie = new QMovie(":/images/loading.gif", QByteArray(), this);
    m_loadingGif->setMovie(m_movie);

    QHBoxLayout *layoutTop = new QHBoxLayout;
    layoutTop->addSpacing(20);
    layoutTop->addWidget(m_labelMessage);
    layoutTop->addWidget(m_loadingGif);
    layoutTop->addSpacing(20);

    // 确定按钮
    m_buttonOk = new MyPushButton;
    m_buttonOk->setText(tr("Ok"));
    m_buttonOk->setProperty("isImportant", true);

    QHBoxLayout *layoutBottom = new QHBoxLayout;
    layoutBottom->addStretch();
    layoutBottom->addWidget(m_buttonOk);

    QVBoxLayout *vlayout = new QVBoxLayout;
    vlayout->addLayout(titleLayout);
    vlayout->addStretch();
    vlayout->addLayout(layoutTop);
    vlayout->addStretch();
    vlayout->addLayout(layoutBottom);

    connect(m_buttonClose, &MyPushButton::clicked, this, &DeleteBackupDialog::close);
    connect(m_buttonOk, &MyPushButton::clicked, this, &DeleteBackupDialog::close);

    setLayout(vlayout);

    deleteBackupPoint();
}

DeleteBackupDialog::~DeleteBackupDialog()
{
    delete ui;
    delete m_pInterface;
}

/**
 * @brief 删除备份点
 * @param backupPonit
 */
void DeleteBackupDialog::deleteBackupPoint()
{
    m_buttonClose->hide();
    m_buttonOk->setEnabled(false);
    m_movie->start();

    GlobelBackupInfo::inst().setIsBusy(true);
    m_pInterface = new ComKylinBackupManagerInterface("com.kylin.backup", "/", QDBusConnection::systemBus(), this);
    connect(m_pInterface, &ComKylinBackupManagerInterface::sendEnvCheckResult, this, &DeleteBackupDialog::on_checkEnv_end);
    connect(m_pInterface, &ComKylinBackupManagerInterface::sendDeleteResult, this, &DeleteBackupDialog::on_deleteBackup_end);

    // 是否已存在备份、还原等操作
    bool isActive = false;
    if(int(BackupState::BACKUP_STATE_INIT) != m_pInterface->getBackupState(isActive)){
        m_labelMessage->setDeplayText(tr("Other backup or restore task is being performed"));
        m_buttonClose->show();
        m_buttonOk->setEnabled(true);
        m_movie->stop();
        m_loadingGif->hide();
        emit deleteFinished(false);

        GlobelBackupInfo::inst().setIsBusy(false);
        disconnect(m_pInterface, &ComKylinBackupManagerInterface::sendEnvCheckResult, this, &DeleteBackupDialog::on_checkEnv_end);
        disconnect(m_pInterface, &ComKylinBackupManagerInterface::sendDeleteResult, this, &DeleteBackupDialog::on_deleteBackup_end);
        delete m_pInterface;
        m_pInterface = nullptr;

        return;
    }

    BackupWrapper backupWrapper;
    backupWrapper.m_backupName = m_backupPonit.m_backupName;
    backupWrapper.m_uuid = m_backupPonit.m_uuid;
    backupWrapper.m_prefixDestPath = m_backupPonit.m_path;
    backupWrapper.m_type = BackupType::DELETE_BACKUP;
    QString udiskPrePath = Utils::getSysRootPath();
    udiskPrePath += "/media";
    udiskPrePath.replace("//", "/");
    backupWrapper.m_iPosition = m_backupPonit.m_iPosition;
    backupWrapper.m_frontUid = getuid();
    backupWrapper.m_gid = getgid();
    m_pInterface->deleteBackupPoint(backupWrapper);
}

/**
 * @brief 环境校验结果
 * @param result
 */
void DeleteBackupDialog::on_checkEnv_end(int result)
{
    bool bRst = false;
    QString errMsg;
    switch (result) {
    case int(BackupResult::LOCK_PROGRAM_FAIL):
        // 程序锁定失败，请重试
        errMsg = tr("Program lock failed, please retry");
        break;
    case int(BackupResult::NO_FOUND_DEALCLASS):
        // 不支持的任务类型
        errMsg = tr("Unsupported task type");
        break;
    default:
        bRst = true;
    }

    if (!bRst) {
        m_labelMessage->setDeplayText(errMsg);
        m_buttonClose->show();
        m_buttonOk->setEnabled(true);
        m_movie->stop();
        m_loadingGif->hide();
        emit deleteFinished(false);

        GlobelBackupInfo::inst().setIsBusy(false);
        disconnect(m_pInterface, &ComKylinBackupManagerInterface::sendEnvCheckResult, this, &DeleteBackupDialog::on_checkEnv_end);
        disconnect(m_pInterface, &ComKylinBackupManagerInterface::sendDeleteResult, this, &DeleteBackupDialog::on_deleteBackup_end);
        delete m_pInterface;
        m_pInterface = nullptr;
    }
}

/**
 * @brief 删除备份点结果
 * @param result
 */
void DeleteBackupDialog::on_deleteBackup_end(bool result)
{
    if (result) {
        // 备份删除成功
        m_labelMessage->setDeplayText(tr("Deleted backup successfully."));
    } else
        m_labelMessage->setDeplayText(tr("Failed to delete backup."));

    m_buttonClose->show();
    m_buttonOk->setEnabled(true);
    m_movie->stop();
    m_loadingGif->hide();
    emit deleteFinished(result);

    GlobelBackupInfo::inst().setIsBusy(false);
    disconnect(m_pInterface, &ComKylinBackupManagerInterface::sendEnvCheckResult, this, &DeleteBackupDialog::on_checkEnv_end);
    disconnect(m_pInterface, &ComKylinBackupManagerInterface::sendDeleteResult, this, &DeleteBackupDialog::on_deleteBackup_end);
    delete m_pInterface;
    m_pInterface = nullptr;
}

void DeleteBackupDialog::paintEvent(QPaintEvent *event)
{
    // 设置背景色
    QPalette palette = this->palette();
    palette.setColor(QPalette::Window, palette.color(QPalette::Base));
    this->setPalette(palette);
    QDialog::paintEvent(event);
}


